package com.negative.activities;

import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.Locale;

import android.os.Bundle;
import android.app.Activity;
import android.content.Context;
import android.content.Intent;

import java.text.DateFormat;

import com.negative.data.EventItem;
import com.negative.data.EventListEntry;
import com.negative.data.Item;
import com.negative.data.SectionItem;
import com.negative.logic.EventListEntryAdapter;
import com.negative.logic.FilterEventByDate;
import com.negative.logic.MenuListener;
import android.text.Html;
import android.view.View;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.ImageView;
import android.widget.ListView;

public class EventListActivity extends Activity {

	private ListView eventList;
	private ArrayList<EventItem> events;
	private EventListEntryAdapter listAdapter;
	private NegativeApplication app;
	private FilterEventByDate eventFilter;
	private Context context;
	private ImageView menuButton;
	private MenuListener menuListener;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.eventlist_layout);
		initialize();
		setList();
	}

	private void setList() {

		DateFormat dateFormat = new SimpleDateFormat("EEEE dd/MM/yyyy", new Locale("iw-il"));
		DateFormat timeFormat = new SimpleDateFormat("kk:mm");

		ArrayList<Item> items = new ArrayList<Item>();

		Date date = null;

		for (int i = 0; i < events.size(); i++) {
			EventItem event = events.get(i);

			Date eDate = event.getSubEvents().get(0).getStart_time();

			if (eventFilter != null
					&& eventFilter.isFilteredEvent(eDate) == true) {
				continue;
			}
			if (date == null
					|| !dateFormat.format(eDate)
							.equals(dateFormat.format(date))) {
				items.add(new SectionItem(dateFormat.format(eDate)));
				date = eDate;

			}

			EventListEntry entry = new EventListEntry(Html.fromHtml(
					event.getName()).toString(), timeFormat.format(eDate),
					event.getPicture(), i);

			items.add(entry);

		}

		listAdapter = new EventListEntryAdapter(context, items);

		eventList.setAdapter(listAdapter);

	}
	
	private void initialize() {
		this.eventFilter = (FilterEventByDate) this.getIntent()
				.getSerializableExtra("filter");
		context = this;

		menuListener = new MenuListener(this);
		
		app = (NegativeApplication) this.getApplication();
		events = app.getEventList();

		eventList = (ListView) findViewById(R.id.event_list_view);
		menuButton = (ImageView) findViewById(R.id.menu_icon);
		
		menuButton.setOnClickListener(menuListener);
		
		eventList.setOnItemClickListener(new OnItemClickListener() {

			@Override
			public void onItemClick(AdapterView<?> parent, View view,
					int position, long id) {
				int i = view.getId();
				EventItem event = events.get(i);

				Intent intent = new Intent(EventListActivity.this,
						EventActivity.class);
				intent.putExtra("EventItem", event);

				startActivity(intent);

			}
		});
	}
}
